package com.tianpengtech.admin.user;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ServiceException;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.opertion.OperationDao;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;

public class UserAdminServiceImpl extends AbstractAdmin implements IAdminOpt{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {

		int status = model.getInt(User.STATUS);
		String mobile = model.getStr(User.MOBILE);
		int BLANK = -1;
		queryConstruct
			.and(new EqualCondition(User.DELETETAG, User.DELETE_STATUS_NO));
		queryConstruct
		.and(new EqualCondition(User.USER_TYPE, User.USER_TYPE_PROV));
		if(StringUtils.isNotEmpty(mobile)){
			queryConstruct
			.and(new LikeCondition(User.MOBILE,"%"+mobile+"%"));
		}
		if(status>BLANK){
			queryConstruct
			.and(new EqualCondition(User.STATUS,status));
		}
		
	}

	@Override
	public String tableName() {
		return User.dao.tableName();
	}

	@Override
	public boolean deleteById(Model<?> model, int id) throws ValidateException, ServiceException {
		
		model.set(User.DELETETAG, User.DELETE_STATUS_YES);
		return model.update();
	}

	@Override
	public void changeStatus(Model<?> model, long id, int status, Kv params) throws ValidateException {
		
		
		Validator validator = new Validator();
		validator.addRule( new NumbericMustBeGreaterThan(id,0,"id值必须要大于0")).excute();
		model.set(User.ID, id);
		model.set(User.STATUS, status);
		model.update();

		Account account = XxxUtil.readSessionInfo();
		long userId = account.getLong(Account.ID);
		User user = User.dao.findById(id);
		if(status==User.USER_STATUS_NORMAL){
			String result = "【解锁】用户【"+user.getStr(User.MOBILE)+"】";
			OperationDao.ME.recordLog(id, Opertion.OPT_USER_LOG,userId,result);
		}
		if(status==User.USER_STATUS_LOCK){
			String result = "【锁定】用户【"+user.getStr(User.MOBILE)+"】";
			OperationDao.ME.recordLog(id, Opertion.OPT_USER_LOG,userId,result);
		}
	}

	
	
	
}